import { Coordinate } from "../coordParser/Coordinate";
import { SymbolConfig } from "./SymbolConfig";
import { parseNum2HexColor } from "../fileLoader/loadSctFile";

export enum DrawCommand {
    MOVETO,
    LINETO,
    ARC,
    FILL_ARC,
    PIXEL,
    POLYGON
}

export class SymbolDrawDefinition {
    command: DrawCommand = DrawCommand.MOVETO;
    coords: Array<Coordinate> = [];
    radius: number = 0;
    start: number = 0;
    end: number = 0;
}

export class SymbolDrawer {
    drawCommand: Array<SymbolDrawDefinition> = [];
    strokePath: Path2D | undefined = undefined;
    fillPath: Array<Path2D> = [];

    draw(context: CanvasRenderingContext2D, coordinate: Coordinate, scaleIndex: number, symbol?: SymbolConfig, rotation?: number) {
        if (symbol) {
            context.strokeStyle = parseNum2HexColor(symbol.color);
            context.fillStyle = parseNum2HexColor(symbol.color);
            context.lineWidth = symbol.lineWeight;
        }
        context.save();
        context.translate(coordinate.longitude * scaleIndex, coordinate.latitude * scaleIndex);
        if (rotation) {
            context.rotate(rotation * (Math.PI / 180));
        }
        if (this.strokePath != undefined || this.fillPath.length > 0) {
            if (this.strokePath != undefined) {
                context.stroke(this.strokePath);
            }
            for (const fp of this.fillPath) {
                context.fill(fp);
            }
            context.restore();
            return;
        }
        console.log('first take')
        const path = new Path2D();
        for (const command of this.drawCommand) {
            if (command.command == DrawCommand.MOVETO) {
                const coord = command.coords[0];
                coord && path.moveTo(coord.longitude, coord.latitude);
            } else if (command.command == DrawCommand.LINETO) {
                const coord = command.coords[0];
                coord && path.lineTo(coord.longitude, coord.latitude);
            } else if (command.command == DrawCommand.ARC) {
                const coord = command.coords[0];
                coord && path.arc(coord.longitude, coord.latitude, command.radius, command.start, command.end);
            } else if (command.command == DrawCommand.FILL_ARC) {
                const coord = command.coords[0];
                if (coord != undefined) {
                    const path = new Path2D;
                    path.arc(coord.longitude, coord.latitude, command.radius, command.start, command.end);
                    context.fill(path);
                    this.fillPath.push(path);
                }
            } else if (command.command == DrawCommand.PIXEL) {
                const coord = command.coords[0];
                coord && path.rect(coord.longitude, coord.latitude, 1, 1);
            } else if (command.command == DrawCommand.POLYGON) {
                const poly = new Path2D;
                for (let i = 0; i < command.coords.length; i++) {
                    const coord = command.coords[i];
                    if (coord == undefined) continue;
                    const x = coord.longitude;
                    const y = coord.latitude;
                    if (i == 0) {
                        poly.moveTo(x, y);
                    } else {
                        poly.lineTo(x, y);
                    }
                }
                context.fill(poly);
                context.restore();
                this.fillPath.push(poly);
            }
        }
        this.strokePath = path;
        context.stroke(this.strokePath);
        context.restore();
    }

    reset() {
        this.drawCommand = [];
    }
}